<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="kr.co.insoft.framework.config.Authorities" %>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<div class="col-sm-12 page-header custom-page-header">
	<h1><spring:message code="menu.configration.cloudconnection"/></h1>	
</div>
<spring:message code="select.seleted.message" var="msgSelect"/>
<form:form modelAttribute="connectionEntityModel" role="form" cssClass="form-horizontal">
	<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
	<form:input path="userid" type="hidden"/>
	<form:input path="regionName" type="hidden"/>
	<form:input path="zoneName" type="hidden"/>
	<form:input path="securityGroupName" type="hidden"/>
	
	<div class="form-group">
		<form:label path="venderid" cssClass="col-sm-2 control-label"><spring:message code="configration.cloud.venderName"/></form:label>
		<div class="col-sm-2">			
			<form:select path="venderid" items="${venderList }" itemLabel="venderName" itemValue="venderid" cssClass="form-control"></form:select>			
		</div>
	</div>
	<div class="form-group">
		<form:label path="accessKey" cssClass="col-sm-2 control-label">Api Key</form:label>
		<div class="col-sm-6">			
			<form:input path="accessKey" cssClass="form-control" />					
		</div>
	</div>
	<div class="form-group">
		<form:label path="secretKey" cssClass="col-sm-2 control-label">Secret Key</form:label>		
		<div class="col-sm-6"> 
			<form:input path="secretKey" cssClass="form-control"/>			
		</div>
		<div class="col-sm-4"> 
			<button class="btn btn-primary btn-sm btnCheckAuth" type="button" >인증</button>
		</div>
	</div>
	<div style="margin-top:50px;"></div> 
	<div class="form-group">
		<form:label path="connectionName" cssClass="col-sm-2 control-label">Connection Name</form:label>		
		<div class="col-sm-10">  
			<form:input path="connectionName" cssClass="form-control"/>			
		</div>
	</div>
	
	<div class="form-group">
		<form:label path="regionSeq" cssClass="col-sm-2 control-label">Region Name</form:label>		
		<div class="col-sm-10"> 
			<form:select path="regionSeq" cssClass="form-control">
				<form:option value="" label="${msgSelect }"/>
			</form:select>			
		</div>
	</div>
	
	<div class="form-group">
		<form:label path="zoneSeq" cssClass="col-sm-2 control-label">Zone Name</form:label>		
		<div class="col-sm-10"> 
			<form:select path="zoneSeq" cssClass="form-control">
				<form:option value="" label="${msgSelect }"/>
			</form:select>		
		</div>
	</div>
	
	<div class="form-group">
		<form:label path="securityGroupid" cssClass="col-sm-2 control-label">Security Group</form:label>		
		<div class="col-sm-10"> 
			<form:select path="securityGroupid" cssClass="form-control">
				<form:option value="" label="${msgSelect }"/>
			</form:select>		
		</div>
	</div>
	
	<div class="form-group">
		<form:label path="keyName" cssClass="col-sm-2 control-label">Key Name</form:label>		
		<div class="col-sm-10"> 
			<form:select path="keyName" cssClass="form-control">
				<form:option value="" label="${msgSelect }"/>
			</form:select>		
		</div>
	</div>
	
	<div class="form-group">
		<form:label path="connectionDesc" cssClass="col-sm-2 control-label">Description</form:label>		
		<div class="col-sm-10"> 
			<form:textarea path="connectionDesc" cssClass="form-control" rows="10"/>			
		</div>
	</div>
	
	<div class="form-group">
		<div class="col-sm-12 text-right">
			<button class="btn btn-default btn_link" data-link="./lists" type="button"><spring:message code="button.list"/></button>
			<c:if test="${fn:contains(buttons , Authorities.CREATE)}">
			<button class="btn btn-primary submitBtn" type="button" onclick="javascript:doSubmit();"><spring:message code="button.save"/></button>
			</c:if> 
		</div>
	</div>	
</form:form>

<script type="text/javascript">

$(document).on("click", ".btnCheckAuth", function() {
	
	bootbox.confirm("<spring:message code='message.certification.confirm'/>", function(result) {
		var venderid = $("#venderid").val();
		var accessKey = $("#accessKey").val();
		var secretKey = $("#secretKey").val();
		
		if(venderid == "" || accessKey == "" || secretKey =="" ){
			bootbox.alert("Api Key 및 Secret Key를 입력해주세요.");
			return;
		}
		
		if ( result ) {
			Loading.show();
			$.get("${CONTEXT}/rest/driver/api/getRegionListByVender.json", {"venderid":venderid, "accessKey":accessKey, "secretKey":secretKey}, function(res) {				
			}).fail(function() {
				bootbox.alert("<spring:message code='common.server.fail'/>"); /* 서버 통신 오류 */
				Loading.hide();
			}).done(function(res) {
				var _opt = "<option value='' selected='selected'><spring:message code='select.seleted.message' /></option>";				
				if(res == null || res == ""){
					
					$("#regionSeq option").remove();
		    		$("#regionSeq").html("<option value='NotSupported' selected='selected'>해당사항없음</option>");
		    		
		    		$("#securityGroupid option").remove();
		    		$("#securityGroupid").html("<option value='NotSupported' selected='selected'>해당사항없음</option>");
		    		
		    		$("#keyName option").remove();
		    		$("#keyName").html("<option value='NotSupported' selected='selected'>해당사항없음</option>");
		    		
					$.get("${CONTEXT}/rest/driver/api/getZoneListByRegion.json", {"regionSeq" : "", "venderid":venderid, "accessKey":accessKey, "secretKey":secretKey }, function(res) {			        	
						var _opt = "<option value='' selected='selected'><spring:message code='select.seleted.message' /></option>";
			            $.each(res, function(index, item) {
			                _opt += "<option value='"+item.zoneName+"'>"+item.zoneName+"("+item.description+")"+"</option>";
			                $("#zoneSeq option").remove();
				            $("#zoneSeq").html(_opt);
			            });			           
			        }).done(function(){
			        	Loading.hide();
			        });
				}else{
					$.each(res,function(idx, item){					
						_opt += "<option value='"+item.regionName+"'>"+item.regionName+"("+item.description+")"+"</option>";
					});
					$("#regionSeq option").remove();
			        $("#regionSeq").html(_opt);
			        Loading.hide();
				}
			});
		}
	});
}).on("change", "#regionSeq", function(){
	var regionSeq = $("#regionSeq option:selected").val();
	if( regionSeq !="") {
        $("#regionName").val($("#regionSeq option:selected").text());
        var venderid = $("#venderid option:selected").val();
    	var accessKey = $("#accessKey").val();
    	var secretKey = $("#secretKey").val();
    	
    	Loading.show();
        $.get("${CONTEXT}/rest/driver/api/getZoneListByRegion.json", {"regionSeq" : regionSeq, "venderid":venderid, "accessKey":accessKey, "secretKey":secretKey }, function(res) {
        	
            var _opt = "<option value='' selected='selected'><spring:message code='select.seleted.message' /></option>";
            $.each(res, function(index, item) {
                _opt += "<option value='"+item.zoneName+"'>"+item.zoneName+"("+item.description+")"+"</option>";
            });
            $("#zoneSeq option").remove();
            $("#zoneSeq").html(_opt);
        }).done(function() {
        	Loading.hide();
        });
        
        $.get("${CONTEXT}/rest/driver/api/getSecurityGroupListByRegion.json", {"regionSeq" : $("#regionSeq option:selected").val(), "venderid":venderid,  "accessKey":accessKey, "secretKey":secretKey }, function(res) {
            
            var _opt = "<option value='' selected='selected'><spring:message code='select.seleted.message' /></option>";
            $.each(res, function(index, item) {
                _opt += "<option value='"+item.groupId+"'>"+item.groupName+"</option>";
            });
            $("#securityGroupid option").remove();
            $("#securityGroupid").html(_opt);
        }).done(function() {
        	Loading.hide();
        });
        
        $.get("${CONTEXT}/rest/driver/api/getKeyPairListByRegion.json", {"regionSeq" : $("#regionSeq option:selected").val(), "venderid":venderid,  "accessKey":accessKey, "secretKey":secretKey }, function(res) {
            var _opt = "<option value='' selected='selected'><spring:message code='select.seleted.message' /></option>";
            $.each(res, function(index, item) {
                _opt += "<option value='"+item.keyName+"'>"+item.keyName+"</option>";
            });
            $("#keyName option").remove();
            $("#keyName").html(_opt); 
        }).done(function() {
        	Loading.hide();
        }); 
	}   
}).on("change", "#zoneSeq", function(){
	$("#zoneName").val($("#zoneSeq").val());
}).on("change", "#securityGroupid", function(){
	$("#securityGroupName").val($("#securityGroupid").val());
}).on("change", "#venderid", function(){
	$("#regionSeq ,#zoneSeq, #securityGroupid, #keyName").find("option").remove();
	//$("#securityGroupRules").remove();
	$("#regionSeq ,#zoneSeq, #securityGroupid, #keyName").html("<option value='' selected='selected'><spring:message code='select.seleted.message' /></option>");
	$("#regionName, #zoneName, #securityGroupName").val("");
	$("#accessKey, #secretKey").val("");
	$("#accessKey, #secretKey").attr("readOnly", false);
	
}).on("click", ".submitBtn", function(){
	bootbox.confirm("<spring:message code='common.save.confirm'/>", function(result) {
		if ( result ) {
			Loading.show();
			$.post("./createForm", $("#connectionEntityModel").serialize(), function(res) {
				
			}).fail(function() {
				bootbox.alert("<spring:message code='common.server.fail'/>"); /* 서버 통신 오류 */
			}).done(function(res) {
				if ( res ) {
					bootbox.alert("<spring:message code='common.save.ok'/>", function() {
						Loading.link("./lists");							
					});
				} else {
					bootbox.alert("<spring:message code='common.save.fail'/>");
				}
			}).always(function() {
				Loading.hide();
			});
		}
	});
});


</script>